#include <gtest/gtest.h>
#include "executor.h"

// 测试1：默认初始化（0,0,N，非加速）
TEST(ExecutorTest, DefaultInitialization) {
    Executor executor;
    int32_t x, y;
    char heading;
    bool isAccel; // 新增加速状态参数，匹配接口
    executor.getCurrentState(x, y, heading, isAccel);

    EXPECT_EQ(x, 0);
    EXPECT_EQ(y, 0);
    EXPECT_EQ(heading, 'N');
    EXPECT_FALSE(isAccel); // 验证默认非加速
}

// 测试2：自定义初始化（指定位置和朝向）
TEST(ExecutorTest, CustomInitialization) {
    Executor executor;
    executor.initialize(5, 3, 'E'); // 初始位置(5,3)，朝向东

    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);

    EXPECT_EQ(x, 5);
    EXPECT_EQ(y, 3);
    EXPECT_EQ(heading, 'E');
    EXPECT_FALSE(isAccel); // 初始化后非加速
}

// 测试3：正常状态下的前进指令
TEST(ExecutorTest, NormalMoveForward) {
    Executor executor;
    executor.initialize(0, 0, 'N'); // 朝北

    // 正常状态：M指令前进1格
    executor.executeCommand('M');
    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);

    EXPECT_EQ(x, 0);
    EXPECT_EQ(y, 1); // 朝北前进1格（Y+）
    EXPECT_EQ(heading, 'N');
    EXPECT_FALSE(isAccel);
}

// 测试4：加速状态下的前进指令
TEST(ExecutorTest, AcceleratedMoveForward) {
    Executor executor;
    executor.initialize(0, 0, 'E'); // 朝东
    executor.executeCommand('F');   // 进入加速状态

    // 加速状态：M指令前进2格
    executor.executeCommand('M');
    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);

    EXPECT_EQ(x, 2); // 朝东前进2格（X+）
    EXPECT_EQ(y, 0);
    EXPECT_EQ(heading, 'E');
    EXPECT_TRUE(isAccel); // 验证加速状态
}

// 测试5：正常状态下的转向（左转+右转）
TEST(ExecutorTest, NormalTurn) {
    Executor executor;
    executor.initialize(0, 0, 'N'); // 初始朝北

    // 正常左转：N→W
    executor.executeCommand('L');
    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);
    EXPECT_EQ(heading, 'W');
    EXPECT_EQ(x, 0); // 位置不变

    // 正常右转：W→N
    executor.executeCommand('R');
    executor.getCurrentState(x, y, heading, isAccel);
    EXPECT_EQ(heading, 'N');
    EXPECT_EQ(x, 0); // 位置不变
}

// 测试6：加速状态下的转向（先前进再转向）
TEST(ExecutorTest, AcceleratedTurn) {
    Executor executor;
    executor.initialize(0, 0, 'N'); // 初始朝北
    executor.executeCommand('F');   // 进入加速状态

    // 加速左转：先前进1格（N→Y+1），再左转（N→W）
    executor.executeCommand('L');
    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);
    EXPECT_EQ(x, 0);
    EXPECT_EQ(y, 1); // 前进1格
    EXPECT_EQ(heading, 'W'); // 左转后朝西

    // 加速右转：先前进1格（W→X-1），再右转（W→N）
    executor.executeCommand('R');
    executor.getCurrentState(x, y, heading, isAccel);
    EXPECT_EQ(x, -1); // 前进1格（朝西）
    EXPECT_EQ(y, 1);
    EXPECT_EQ(heading, 'N'); // 右转后朝北
}
// 测试7：加速状态切换（F指令开关）
TEST(ExecutorTest, AccelerationToggle) {
    Executor executor;
    bool isAccel;
    int32_t dummy_x, dummy_y;  // 声明临时变量接收不需要的参数
    char dummy_heading;
    
    // 第一次F：开启加速（使用临时变量传递参数）
    executor.executeCommand('F');
    executor.getCurrentState(dummy_x, dummy_y, dummy_heading, isAccel);
    EXPECT_TRUE(isAccel);
    
    // 第二次F：关闭加速
    executor.executeCommand('F');
    executor.getCurrentState(dummy_x, dummy_y, dummy_heading, isAccel);
    EXPECT_FALSE(isAccel);
}

// 测试8：批量指令执行（混合正常+加速）
TEST(ExecutorTest, BatchCommands) {
    Executor executor;
    executor.initialize(0, 0, 'N');

    // 指令序列：F(加速)→M(前2)→L(前1+左转)→F(取消)→M(前1)
    executor.executeCommands("FMFLM");

    int32_t x, y;
    char heading;
    bool isAccel;
    executor.getCurrentState(x, y, heading, isAccel);

    // 计算过程：
    // F→加速；M→前2（Y=2）；L→前1（Y=3）+左转（N→W）；F→取消加速；M→前1（W→X=-1）
    EXPECT_EQ(x, -1);
    EXPECT_EQ(y, 3);
    EXPECT_EQ(heading, 'W');
    EXPECT_FALSE(isAccel);
}

// 主函数：运行所有测试
int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}